package com.example.utils.image;

import com.example.utils.StringUtils;

public class AutoPlayerImageThread implements Runnable {

    public void run() {
        // 创建线程 那么active设置为true
        Temp.active = true;
        for(int i=0;i<5000;i++){
            if(Temp.next){
                try {
                    Thread.sleep(Temp.time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String [] arr = ImageUtils.getNextRoundImage();
                String fileName = arr[0];
                if(StringUtils.isNotEmpty(fileName)){
                    Temp.CQ.sendGroupMsg(511205024L," 已经播放【"+arr[1]+"】张"+"\n"+image(fileName));
                }
            }else{
                Temp.active = false;
            }
        }
    }

    public static String encode(String code, boolean isComma) {
        code = org.meowy.cqp.jcq.util.StringUtils.stringReplace(code, "&", "&amp;");
        code = org.meowy.cqp.jcq.util.StringUtils.stringReplace(code, "[", "&#91;");
        code = org.meowy.cqp.jcq.util.StringUtils.stringReplace(code, "]", "&#93;");
        if (isComma) {
            code = org.meowy.cqp.jcq.util.StringUtils.stringReplace(code, ",", "&#44;");
        }

        return code;
    }

    public String image(String file) {
        return org.meowy.cqp.jcq.util.StringUtils.stringConcat(new Object[]{"[CQ:image,file=", encode(file, true), "]"});
    }
}
